import { useState } from 'react'
import { View, StyleSheet } from 'react-native'
import { CameraView } from 'expo-camera/next'
import { useRouter } from 'expo-router'

export default function CameraPage() {
  const router = useRouter();
  const [hasPermission, setHasPermission] = useState(null);
  const [scanned, setScanned] = useState(false);

  const handleBarcodeScanned = (option) => {
    setScanned(true)
    router.replace({pathname: `/?data=${encodeURIComponent(option.data)}`})
  }
  return (
    <View style={styles.container}>
      <CameraView
         style={styles.camera}
         onBarcodeScanned={scanned ? undefined : handleBarcodeScanned}
         barcodeScannerSetting={{
          barcodeTypes: []
         }}
      />
    </View>
  )
}

const styles = StyleSheet.create({
  container: {
    flex: 1
  },
  camera: {
    flex: 1
  }
})